//
//  OADPlainTableViewController.m
//  OASDK_Works
//
//  Created by ChenJie on 3/31/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADPlainTableViewController.h"
#import "OABase.h"
#import "OADMocker.h"
#import "OADefaultCell.h"
#import "RETProductDescriptionCell.h"

@interface OADPlainTableViewController ()
<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, strong) NSMutableArray *cells;
@property (nonatomic, strong) NSMutableArray *cellClasses;
@property (nonatomic, strong) NSMutableDictionary *cellComponentFrames;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation OADPlainTableViewController

#pragma mark - ViewController lifecycle

- (instancetype)init {
    self = [super init];
    if (self) {
        [self buildDataSource];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass(self.class);
    
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGFloat topMark = 0;
    CGSize viewSize = self.view.bounds.size;
    
    self.tableView.frame = CGRectMake(0, topMark, viewSize.width, viewSize.height - topMark);
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - Public methods



#pragma mark - Private methods

- (void)buildDataSource {
    
    RETProductDescriptionCellViewModel *productDescriptionCellViewModel = [RETProductDescriptionCellViewModel new];
    productDescriptionCellViewModel.text = [OADMocker mockTitleWithMinLength:20 maxLength:50];
    [self.cells addObject:productDescriptionCellViewModel];
    [self.cellClasses addObject:RETProductDescriptionCell.class];
    
    for (NSInteger i = 0; i < 4; i++) {
        OADefaultCellViewModel *cellViewModel = [OADefaultCellViewModel new];
        cellViewModel.title = [OADMocker mockTitle];
        cellViewModel.content = [OADMocker mockContent];
        [self.cells addObject:cellViewModel];
        [self.cellClasses addObject:OADefaultCell.class];
    }
}

#pragma mark - Event response methods

- (void)buttonClicked:(UIButton *)button {
    NSLog(@"button clicked...");
}

#pragma mark - Deletate methods



#pragma mark - <UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cells.count;
}

#pragma mark - <UITableViewDelegate>

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"cmd: %@, width: %f", NSStringFromSelector(_cmd), tableView.oa_width);
    
    Class cellClass = self.cellClasses[indexPath.row];
    id<OACellViewModelProtocol> cellViewModel = self.cells[indexPath.row];
    
    if (tableView.oa_width == 0) {
        return 0;
    }
    
    return [cellClass oa_heightWithWidth:tableView.oa_width viewModel:cellViewModel];
    
//    NSDictionary *componentFrames = [cellClass oa_componentFramesWithCellWidth:tableView.oa_width
//                                                                     viewModel:cellViewModel];
//    self.cellComponentFrames[indexPath] = componentFrames;
//    return [cellClass oa_cellHeightWithComponentFrames:componentFrames];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    NSLog(@"cmd: %@, width: %f", NSStringFromSelector(_cmd), tableView.oa_width);
    
    Class cellClass = self.cellClasses[indexPath.row];
    NSString *cellIdentifier = NSStringFromClass(cellClass);
    
    UITableViewCell<OACellProtocol> *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    id<OACellViewModelProtocol> cellViewModel = self.cells[indexPath.row];
    cell.viewModel = cellViewModel;
//    cell.componentFrames = self.cellComponentFrames[indexPath];
    
//    [cell oa_showBounds];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

#pragma mark - Setter



#pragma mark - Getter

OAGetMutableArray(cells)

OAGetMutableArray(cellClasses)

OAGetMutableDictionary(cellComponentFrames)

OAGetTableView(tableView, ^(UITableView *tableView){
    tableView.dataSource = self;
    tableView.delegate = self;
})

#pragma mark - Memory management

- (void)dealloc {
}

@end
